<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/10/22
 * Time: 11:08
 */

namespace app\mobileweb\controller;

use app\mobileweb\controller\BaseWeb;
use app\src\user\action\LoginAction;
use think\Controller;
use app\src\base\enum\ErrorCode;
use app\src\base\helper\PageHelper;
use app\src\base\helper\ValidateHelper;
use app\src\base\utils\CryptUtils;
use app\src\user\facade\DefaultUserFacade;
use app\src\extend\Page;
use think\Db;
use think\Response;
use app\src\base\utils\BoyeService;


abstract class Webview extends BaseWeb{

    protected $uid;
    protected $user;
    protected $psw;
    protected $country;
    //需要验证用户密码
    protected $needValid = true;
    
    public function _initialize() {
        $userinfo=session('userinfo');
        $this -> uid =  isset($userinfo['id'])?$userinfo['id']:'';
        $this -> user = isset($userinfo['mobile'])?$userinfo['mobile']:'';
        $this->auto_code=isset($userinfo['auto_login_code'])?$userinfo['auto_login_code']:'';
        $this -> psw =  isset($userinfo['password'])?$userinfo['password']:'';
        $this -> country =isset($userinfo['country_no'])?$userinfo['country_no']:'';
        $this -> valid();
        
    }

    //用户密码验证
    protected function valid(){
        if(!$this->needValid) return;

        if(empty($this->uid)){
            $this->uid=152;
            $this->psw=123456;
            $this->auto_code='47a9128f267d3ceac8c2d66a5a94e99f';
            session('userinfo',['id'=>152,'password'=>123456]);
            }

        if($this->uid <= 0 || $this->psw == ''){
            $this->redirect('Login/login');
        }
        $uid=$this->uid;
        $auto_login_code=$this->auto_code;
        $result = (new DefaultUserFacade())->autoLogin($uid,$auto_login_code);
        if(!$result['status']||empty($result['info'])) $this->error('验证登录错误','__PUBLIC__/Login/login');

//        //验证登录
//        $login=new LoginAction();
//        $user=$this->user;
//        $psw=$this->psw;
//        $country=$this->country;
//        $device_token = $this->_post('device_token','');
//        $device_type  = 'pc';
//        $login_done=$login->login($user,$psw,$country,$device_token,$device_type);
//        if(!$login_done['status']) $this->error('验证登录错误','http://127.0.0.1/github/itboye_guannan_api/public/mobileweb.php/Login/login');
    }



    /**
     * 获取分页参数信息
     * @return PageHelper
     */
    public function getPageParams(){
        return new PageHelper([
            'page_index'=>$this->_post('page_index',1),
            'page_size'=>$this->_post('page_size',10)
        ]);
    }



    protected function apiReturnErr($data) {
//        addLog($this->domain_class, $code, $data, '返回错误');
        $this->error($data);
    }


    /**
     * ajax返回
     * @param $data
     * @internal param $i
     */
    protected function apiReturnSuc($data) {
//        addLog($this->domain_class, $this->api_ver, $data, '返回成功', true);

        $this->success($data);
    }

    /**
     * @param $key
     * @param string $default
     * @param string $emptyErrMsg 为空时的报错
     * @return mixed
//     */
//    public function _post($key, $default = '', $emptyErrMsg = '') {
//            $value=input($key,$default);
//        return $value;
//
//    }
    /**
     * 放到utils中，作为工具类
     * @brief 干掉emoji
     * @autho chenjinya@baidu.com
     * @param {String} $strText
     * @return {String} removeEmoji
     **/
    protected function escapeEmoji($strText, $bool = false) {
        $preg = '/\\\ud([8-9a-f][0-9a-z]{2})/i';
        if ($bool == true) {
            $boolPregRes = (preg_match($preg, json_encode($strText, true)));
            return $boolPregRes;
        } else {
            $strPregRes = (preg_replace($preg, '', json_encode($strText, true)));
            $strRet = json_decode($strPregRes, JSON_OBJECT_AS_ARRAY);

            if ( is_string($strRet) && strlen($strRet) == 0) {
                return "";
            }

            return $strRet;
        }
    }


    protected function getuid() {
        $user=session('userinfo');
        if(empty($user)) $this->error('获取用户信息失败');
        $uid=$user['id'];
        return $uid;
    }










}